//
//  YGEmoticonToolBar.swift
//  表情键盘
//
//  Created by liyonggui on 16/5/23.
//  Copyright © 2016年 liyonggui. All rights reserved.
//

import UIKit

// 定义代理协议
protocol YGEmoticonToolBarDelegate: NSObjectProtocol {
    // 代理方法
    func didSelectedButtonType(didSelectedType: YGEmoticonToolBarType)
}

/// 类型
enum YGEmoticonToolBarType: Int {
    case Recent     // 最近
    case Default    // 默认
    case Emoji      // emoji
    case Lxh        // 浪小花
}

class YGEmoticonToolBar: UIView {
    
    // MARK: - 属性
    /// 按钮数组
    private var buttons = [UIButton]()
    
    /// 保存当前选中的按钮
    private var selectedButton: UIButton?
    
    /// 代理属性
    weak var delegate: YGEmoticonToolBarDelegate?

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }

    private func prepareUI() {
        backgroundColor = UIColor.lightGrayColor()
        
        // 添加四个按钮
        let titles = ["最近", "默认", "emoji", "浪小花"]
        var index = 0
        for title in titles {
            let button = UIButton()
            // compose_emotion_table_mid_normal
            button.setTitle(title, forState: UIControlState.Normal)
            button.setBackgroundImage(UIImage(named: "compose_emotion_table_mid_normal"), forState: UIControlState.Normal)
            button.setBackgroundImage(UIImage(named: "compose_emotion_table_mid_selected"), forState: UIControlState.Highlighted)
            button.setBackgroundImage(UIImage(named: "compose_emotion_table_mid_selected"), forState: UIControlState.Selected)
            // tag
            button.tag = index
            // 添加点击事件
            button.addTarget(self, action: "didClickButton:", forControlEvents: UIControlEvents.TouchUpInside)
            // 添加到父容器
            addSubview(button)
            // 添加到数组
            buttons.append(button)
            
            index++
        }
        
        // 默认选中最近按钮
        let defaultButton = buttons[0]
        switchSelectedButton(defaultButton)
    }
    
    // 布局frame
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 计算宽度
        let width = frame.width / CGFloat(buttons.count)
        
        var index = 0
        
        for button in buttons {
            let frame = CGRect(x: width * CGFloat(index), y: 0, width: width, height: self.frame.height)
            button.frame = frame
            index++
        }
    }
    
    
    // MARK: - 点击事件
    @objc private func didClickButton(button: UIButton) {
        let type = YGEmoticonToolBarType(rawValue: button.tag)!
        switchSelectedButton(button)
        
        // 调用代理方法
        delegate?.didSelectedButtonType(type)
    }
    
    // MARK: - 公开的方法
    ///  根据下标切换选中按钮
    ///
    ///  - parameter section: 下标
    func switchSelectedButtonWithSelection(section: Int) {
        // 获取对应的button
        let button = buttons[section]
        switchSelectedButton(button)
    }
    
    ///  切换选中的按钮
    ///
    ///  - parameter button: 要选中的按钮
    private func switchSelectedButton(button: UIButton) {
        if button == selectedButton {
            return
        }
        selectedButton?.selected = false
        button.selected = true
        selectedButton = button
    }
    

}


































